/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_httpd.htm
 *
 * Zhiqim Httpd is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.httpd.context.config;

import org.zhiqim.httpd.context.ZmlConfig;
import org.zhiqim.httpd.context.ZmlContextConstants;
import org.zhiqim.kernel.model.Filter;
import org.zhiqim.kernel.model.FilterHandler;
import org.zhiqim.kernel.model.Filters;
import org.zhiqim.kernel.util.Resources;

/**
 * ActionPackage配置
 *
 * @version v1.0.0 @author zhichenggang 2016-12-31 新建与整理
 */
public class ZActionPackage implements ZmlContextConstants
{
    private final ZmlConfig zmlConfig;
    
    private final String actionPackage;
    private final ZActionMap actionMap;
    
    public ZActionPackage(ZmlConfig zmlConfig, ZConfig config, String actionPackage)
    {
        this.zmlConfig = zmlConfig;
        this.actionPackage = actionPackage;
        this.actionMap = new ZActionMap(zmlConfig, config);
    }
    
    public void load()
    {
        Filter filter = new Filters.EndsWithName(".class");
        FilterHandler handler = new ZActionPackageLoader(this);
        //扫描全路径
        Resources.scanClassPath(filter, handler);
    }

    public String getActionPackage()
    {
        return actionPackage;
    }

    public void addActionOnPackage(ZAction action)
    {
        actionMap.addAction(action);
    }

    public ZmlConfig getConfig()
    {
        return zmlConfig;
    }
    
    /*****************************************************************************/
    //通过path 获取 ZAction
    /*****************************************************************************/
    
    public ZAction getInTurnAction(String path)
    {
        //先查本文件精确再查模糊匹配
        ZAction action = actionMap.getExactAction(path);
        if (action != null)
            return action;
        
        return actionMap.getFuzzyAction(path);
    }
    
    public ZAction getExactAction(String path)
    {
        return actionMap.getExactAction(path);
    }
    
    public ZAction getFuzzyAction(String path)
    {
        return actionMap.getFuzzyAction(path);
    }
    
    /*****************************************************************************/
    //通过class 获取 ZAction
    /*****************************************************************************/
    
    public ZAction getInTurnActionByClass(String clazz)
    {
        //先查本文件精确再查模糊匹配
        ZAction action = actionMap.getExactActionByClass(clazz);
        if (action != null)
            return action;
        
        return actionMap.getFuzzyActionByClass(clazz);
    }
    
    public ZAction getExactActionByClass(String clazz)
    {
        return actionMap.getExactActionByClass(clazz);
    }
    
    public ZAction getFuzzyActionByClass(String clazz)
    {
        return actionMap.getFuzzyActionByClass(clazz);
    }
}
